package com.mirce.controller;

import com.mirce.mapper.userMapper;
import com.mirce.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class hh {

    @Autowired
    userMapper usermapper;

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @PostMapping("/login")
    public String hh(String username,String password,Model model){

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        try
        {
            subject.login(usernamePasswordToken);
            return "message.html";
        } catch(UnknownAccountException uae){
            model.addAttribute("msg","用户名不存在");
            return "index.html";
        }catch (IncorrectCredentialsException ice){
            model.addAttribute("msg","密码不正确");
            return "index.html";
        }catch (LockedAccountException lae){
            model.addAttribute("msg","账户已被锁定");
            return "index.html";
        }
    }

    @GetMapping("/info")
    public String info(Model model){
        Object fromName = SecurityUtils.getSubject().getPrincipal();
        User user=usermapper.queryUser((String) fromName);
        model.addAttribute("name",fromName);
        model.addAttribute("age",user.getAge());
        model.addAttribute("sex",user.getSex());
        model.addAttribute("address",user.getAddress());
        model.addAttribute("phone",user.getPhone());
        return "account/info.html";
    }

    @RequestMapping("/register")
    public String register(){
        return "account/register.html";
    }

    @RequestMapping("/contract")
    public String websocket(){

        return "contract.html";
    }

    @RequestMapping("/message")

    public String message(Model model){

        return "message.html";


    }

    @RequestMapping("/chat")
    public String chatting(Model model){
        Object fromName = SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("username",fromName);
        //model.addAttribute("tousername",name);
        return "chat.html";
    }

    @PostMapping("/infoSave")
    public String infoSave(HttpServletRequest request,Model model){

        String name =request.getParameter("name");
        String age =request.getParameter("age");
        String sex =request.getParameter("sex");
        String address =request.getParameter("address");
        String phone =request.getParameter("phone");


        Map<String, String> map = new HashMap<>();
        map.put("name",name);
        map.put("age",age);
        map.put("sex",sex);
        map.put("address",address);
        map.put("phone",phone);
        usermapper.updateage(map);
        usermapper.updatesex(map);
        usermapper.updateaddress(map);
        usermapper.updatephone(map);

        User user=usermapper.queryUser(name);
        model.addAttribute("name",name);
        model.addAttribute("age",user.getAge());
        model.addAttribute("sex",user.getSex());
        model.addAttribute("address",user.getAddress());
        model.addAttribute("phone",user.getPhone());

        return "account/info.html";
    }

}


